<!DOCTYPE html>
<html>
	<head>
		<script src="js/core-min.js"></script>
		<script src="js/hmac-min.js"></script>
		<script src="js/sha256-min.js"></script>
		<script src="js/pbkdf2-min.js"></script>
		<script src="js/jquery-min.js"></script>
		<link rel="shortcut icon" href="icon16.png" />
		<script type="text/javascript">
			// orion elenzil
			// 20080905
			 
			function getValueOfDigit(digit, alphabet)
			{
			   var pos = alphabet.indexOf(digit);
			   return pos;
			}
			 
			function convert(src, srcAlphabet, dstAlphabet)
			{
			   var srcBase = srcAlphabet.length;
			   var dstBase = dstAlphabet.length;
			 
			   var wet     = src;
			   var val     = 0;
			   var mlt     = 1;
			 
			   while (wet.length > 0)
			   {
				 var digit  = wet.charAt(wet.length - 1);
				 val       += mlt * getValueOfDigit(digit, srcAlphabet);
				 wet        = wet.substring(0, wet.length - 1);
				 mlt       *= srcBase;
			   }
			 
			   wet          = val;
			   var ret      = "";
			 
			   while (wet >= dstBase)
			   {
				 var digitVal = wet % dstBase;
				 var digit    = dstAlphabet.charAt(digitVal);
				 ret          = digit + ret;
				 wet /= dstBase;
			   }
			 
			   var digit    = dstAlphabet.charAt(wet);
			   ret          = digit + ret;
			   
			   return ret;
			}
		</script>
		<script type="text/javascript">
			// Generate password.
			var worker = null;
			function getPassWithWorker(master, salt, cb) {
				if (worker != null) {
					worker.terminate();
					worker = null;
				}
				worker = new Worker('worker.js');
				worker.onmessage = function(e) {
					worker.terminate();
					worker = null;
					cb(e.data);
				};
				worker.postMessage({master: master, salt: salt});
			}
			
			var timeout = null;
			function getPassWithTimeout(master, salt, cb) {
				if (timeout != null) {
					clearTimeout(timeout);
					timeout = null;
				}
				timeout = setTimeout(function () {
					var hash = CryptoJS.PBKDF2(master, salt, { keySize: 256/32, iterations: 1000, hasher: CryptoJS.algo.SHA256 }).toString();
					var fullpass = convert(hash,"0123456789abcdef","0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
					var pass = "";
					var chars = 0;
					for (i = 0; chars < 12; i++) {
						j = i % 12;
						if (chars == 0 && fullpass.substring(j,j+1).toUpperCase() != fullpass.substring(j,j+1)) {
							pass += fullpass.substring(j,j+1);
							chars++;
						} else if (chars == 1 && parseInt(fullpass.substring(j,j+1)) == fullpass.substring(j,j+1)) {
							pass += fullpass.substring(j,j+1);
							chars++;	
						} else if (chars == 2 && fullpass.substring(j,j+1).toLowerCase() != fullpass.substring(j,j+1)) {
							pass += fullpass.substring(j,j+1);
							chars++;
						} else if (chars > 2) {
							pass += fullpass.substring(j,j+1);
							chars++;						
						} else if (chars == 0 && i >= fullpass.length) {
							pass += "f";
							chars++
						} else if (chars == 1 && i >= fullpass.length*2) {
							pass += "7";
							chars++
						} else if (chars == 2 && i >= fullpass.length*3) {
							pass += "T";
							chars++
						}
					}
					cb(pass);
				}, 1000);
			}

			// Page functionality.
			function update() {
				$('#output').addClass('hide');
				if ($('#password').val().length < 3 || $('#salt').val().length < 1) {
					$('#output').text("Enter a master password and site key to generate your password.")
				} else {
					$('#output').text("Generating...")
					if (window.Worker) {
						getPassWithWorker($('#password').val(),$('#salt').val(), function (pass) {
							$('#output').html("Password to use<br /><span>"+pass+"</span><br /><label><input id='show' type='checkbox' /> Show password</label>");
						})
					} else {
						getPassWithTimeout($('#password').val(),$('#salt').val(), function (pass) {
							$('#output').html("Password to use<br /><span>"+pass+"</span><br /><label><input id='show' type='checkbox' /> Show password</label>");
						})					
					}
				}
			}
			
			$(function () {
				$('#password').keyup(update);
				$('#salt').keyup(update);
				$('#show').live('change', function () {
					!$('#show').is(':checked') ? $("#output").addClass('hide') : $("#output").removeClass('hide');
				});
				update();
			});
		</script>
		<style>
			body {
				width: 800px;
				margin: 0 auto;
				font-family: sans-serif;
				font-size: 1.6em;
				text-align: center;
			}
			input {
				font-size: 1.3em;
			}
			.box {
				float: left;
				width: 50%;
			}
			#output {
				clear: both;
				padding-top: 40px;
			}
			#output span {
				font-size: 2.2em;
				font-family: monospace;
				border: 1px solid grey;
			}
			.hide span {
				color: white;
			}
			.hide span::-webkit-selection, .hide span::selection {
				color: #343234;
				background: #000;
			}
			.hide span::-moz-selection {
				color: #000;
				background: #000;
			}
			#details {
				font-size: 0.6em;
			}
			#header {
				text-align: left;
				padding: 30px;
			}
			#right {
				float: right;
				padding: 35px 20px;
				text-align: right
			}
			a:link, a:visited {
				color: #7e3f10;
				text-decoration: none;
			}
			a:hover, a:active {
				text-decoration: underline;
			}
			#update {
				font-size: 0.7em;
				margin-bottom: 10px;
				border: 1px dashed #888;
				padding: 10px;
			}
		</style>
		<title>PassHash</title>
	</head>
	<body>
		<div id="header">
			<img src="icon256.png" />
			<div id="right">
				<a href="/">Generate passwords</a><br /><br />
				<a href="https://chrome.google.com/webstore/detail/ebkoipncioolddgipnflfmidgdhobjdh">Chrome extension</a><br />
				<a href="https://market.android.com/details?id=uk.co.connorhd.passhash">Android app</a><br /><br />
				<a href="/help.htm">Help</a>
			</div>	
		</div>
		<div class="box">
			Master password<br />
			<input type="password" id="password" value="" />
		</div>
		<div class="box">
			Site key (e.g. amazon.com)<br />
			<input type="text" id="salt" value="" />
		</div>
		<div id="output" class="hide"></div>
		<div id="details">
			<p>Be careful when typing your master password on an untrusted computer.</p>
			<p>Remember to always clear your clipboard if you copy the password.</p>
			<p>Don't know what this is? <a href="/help.htm">Click here for help.</a>
		</div>
		<div id="update">Algorithm updated 2012-06-10, to generate old PassHash passwords <a href="v1">click here</a></div>
	</body>
</html>
